package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.interfaces.DWaybillApplyResultService;
import cn.huiyunche.base.service.mappers.DWaybillApplyResultMapper;
import cn.huiyunche.base.service.model.DWaybillApplyResult;
import cn.huiyunche.base.service.model.DWaybillApplyResultExample;
import org.apache.commons.collections4.CollectionUtils;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @FileName: cn.huiyunche.app.service.interfaces.impl
 * @Description: Description
 * @author: Aaron
 * @date: 2016/12/26 下午2:57
 */
@Service
public class DWaybillApplyResultServiceImpl implements DWaybillApplyResultService {

    private static final Logger LOGGER = LoggerFactory.getLogger(DWaybillApplyResultServiceImpl.class);

    @Autowired
    private DWaybillApplyResultMapper dWaybillApplyResultMapper;

    @Override
    public Long add(DWaybillApplyResult result) {
        LOGGER.info("add param : {}", result);
        if (null == result) {
            LOGGER.error("add param result must not null");
            throw new IllegalArgumentException("申请记录不能为空");
        }

        dWaybillApplyResultMapper.insertSelective(result);
        return result.getId();
    }

    @Override
    public Long update(DWaybillApplyResult result) {
        LOGGER.info("update param : {}", result);
        if (null == result) {
            LOGGER.error("update param result must not be null");
            throw new IllegalArgumentException("申请记录不能为空");
        }

        dWaybillApplyResultMapper.updateByPrimaryKeySelective(result);
        return null;
    }

    @Override
    public Long totalOfApply(Long userId, Boolean isApplySuccess, Long routeId) {
        LOGGER.info("isExceededTheDailyMaximum param : {}", routeId);
        if (null == routeId) {
            LOGGER.error("totalOfApply param routeId must not be null");
            throw new IllegalArgumentException("线路主键不能为空");
        }

        List<DWaybillApplyResult> list = this.getListByIsApplySuccess(isApplySuccess, routeId, userId);
        if (CollectionUtils.isNotEmpty(list)) {
            if (isApplySuccess) {
                return list.stream().mapToLong(r -> r.getActualAmount()).sum();
            } else {
                return list.stream().mapToLong(r -> r.getApplyAmount()).sum();
            }
        }

        return 0L;
    }

    @Override
    public List<DWaybillApplyResult> getListByIsApplySuccess(boolean isApplySuccess, Long routeId, Long userId) {
        LOGGER.info("getListByIsApplySuccess param : {}", isApplySuccess);

        DWaybillApplyResultExample example = new DWaybillApplyResultExample();
        example.createCriteria()
                .andCreateTimeGreaterThanOrEqualTo(new DateTime().withMillisOfDay(0).toDate())//当日0点
                .andCreateTimeLessThan(new DateTime().withMillisOfDay(0).plusDays(1).toDate())//第二天0点
                .andIsApplySuccessEqualTo(isApplySuccess)
                .andRouteIdEqualTo(routeId)
                .andUserIdEqualTo(userId);

        List<DWaybillApplyResult> list = dWaybillApplyResultMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(list)) {
            return list;
        }
        return null;
    }
}
